/*
 * CProcItem.java
 * 
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 * 
 * This file is part of JRogueDPS.
 * 
 * JRogueDPS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JRogueDPS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with JRogueDPS.  If not, see <http://www.gnu.org/licenses/>.
 */

package geardatabase;

import geardatabase.CGemType.GemType;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Element;
import org.dom4j.Node;
import utility.CUtility;

/**
 *
 * @author Giles Millward (Manigra)
 */
public class CProcItem {
    public String m_name;
    public int m_id;

    // Attack types that proc item is valid for
    public boolean m_whiteAttack = false;
    public boolean m_specialAttack = false;
    public boolean m_meleeFinisher = false;
    public boolean m_spellFinisher = false;
    public boolean m_deadlyPoison = false;
    public boolean m_instantPoison = false;
    public boolean m_woundPoison = false;
    public boolean m_anestheticPoison = false;
    public boolean m_rupture = false;
    public boolean m_onHit = false;
    public boolean m_onCrit = false;

    public double  m_ppm; // Procs per minute
    public double  m_procChance; // Chance to proc
    public double  m_procLength; // seconds
    public int     m_internalCDLength; // internal cooldown length (0 = no cooldown)
    public boolean m_stackable; // Set to false if the proc effect cannot stack with another
                                // enchant of the same id
    public boolean m_onUse;     // Is an on use trinket? (Not really a proc as such but it needs to be triggered)
    public int     m_maxStackSize;    // Maximum number of stacks it can grow to
    public int     m_onUseInitialDelay; // Initial delay before use of proc

    public double  m_damageProc; // Damage type proc
    public boolean m_physicalDamageProc; // Physical or spell damage proc

    public CStats  m_stats; // Stats to add when proc happens

    public CProcItem(Element p_element)
    {
        m_stats = new CStats();
        this.parseXMLElement(p_element);
    }

    CProcItem(CItem p_item, double p_ppm, double p_procChance, double p_procLength, int p_internalCDLength,
                boolean p_stackable,
                boolean p_onUse,
                int     p_onUseInitialDelay,
                int     p_maxStackSize,
                double  p_damageProc,
                boolean p_physicalDamageProc,
                boolean p_whiteAttack,
                boolean p_specialAttack,
                boolean p_meleeFinisher,
                boolean p_spellFinisher,
                boolean p_deadlyPoison,
                boolean p_instantPoison,
                boolean p_woundPoison,
                boolean p_anestheticPoison,
                boolean p_rupture,
                boolean p_onHit,
                boolean p_onCrit,
                CStats p_stats
                )
    {
        m_name = p_item.getName();
        m_id = p_item.getId();
        m_ppm = p_ppm;
        m_procChance = p_procChance;
        m_procLength = p_procLength;
        m_stackable = p_stackable;
        m_internalCDLength = p_internalCDLength;
        m_onUse = p_onUse;
        m_maxStackSize = p_maxStackSize;
        m_onUseInitialDelay = p_onUseInitialDelay;

        m_damageProc = p_damageProc;
        m_physicalDamageProc = p_physicalDamageProc;

        m_whiteAttack = p_whiteAttack;
        m_specialAttack = p_specialAttack;
        m_meleeFinisher = p_meleeFinisher;
        m_spellFinisher = p_spellFinisher;
        m_deadlyPoison = p_deadlyPoison;
        m_instantPoison = p_instantPoison;
        m_woundPoison = p_woundPoison;
        m_anestheticPoison = p_anestheticPoison;
        m_rupture = p_rupture;
        m_onHit = p_onHit;
        m_onCrit = p_onCrit;
        m_stats = p_stats;
    }

    public void addXMLData(Element p_root)
    {
        // Add item id
        Element l_item = p_root.addElement("procitem").
                        addAttribute("id", String.valueOf(this.m_id)).
                        addAttribute("name", this.m_name).
                        addAttribute("ppm", String.valueOf(this.m_ppm)).
                        addAttribute("procchance", String.valueOf(m_procChance)).
                        addAttribute("proclength", String.valueOf(m_procLength)).
                        addAttribute("stackable", CUtility.boolToString(m_stackable)).
                        addAttribute("internalcdlength", String.valueOf(m_internalCDLength)).
                        addAttribute("onuse", CUtility.boolToString(m_onUse)).
                        addAttribute("maxstacksize", String.valueOf(m_maxStackSize)).
                        addAttribute("onuseinitialdelay", String.valueOf(m_onUseInitialDelay)).
                        addAttribute("damageproc", String.valueOf(m_damageProc)).
                        addAttribute("physicalDamageProc", CUtility.boolToString(m_physicalDamageProc)).
                        addAttribute("whiteattack", CUtility.boolToString(m_whiteAttack)).
                        addAttribute("specialattack", CUtility.boolToString(m_specialAttack)).
                        addAttribute("meleefinisher", CUtility.boolToString(m_meleeFinisher)).
                        addAttribute("spellfinisher", CUtility.boolToString(m_spellFinisher)).
                        addAttribute("deadlypoison", CUtility.boolToString(m_deadlyPoison)).
                        addAttribute("instantpoison", CUtility.boolToString(m_instantPoison)).
                        addAttribute("woundpoison", CUtility.boolToString(m_woundPoison)).
                        addAttribute("anestheticpoison", CUtility.boolToString(m_anestheticPoison)).
                        addAttribute("rupture", CUtility.boolToString(m_rupture)).
                        addAttribute("onhit", CUtility.boolToString(m_onHit)).
                        addAttribute("oncrit", CUtility.boolToString(m_onCrit));
        m_stats.addXMLData(l_item);
    }

    private void parseXMLElement(Element p_element)
    {
        // p_element has our item ptr in it.
        this.m_id = Integer.parseInt(p_element.attributeValue("id"));
        this.m_name = p_element.attributeValue("name");
        this.m_ppm = Double.parseDouble(p_element.attributeValue("ppm"));
        this.m_procChance = Double.parseDouble(p_element.attributeValue("procchance"));
        this.m_procLength = Double.parseDouble(p_element.attributeValue("proclength"));
        this.m_stackable = CUtility.stringToBool(p_element.attributeValue("stackable"));
        this.m_internalCDLength = Integer.parseInt(p_element.attributeValue("internalcdlength"));
        this.m_onUse = CUtility.stringToBool(p_element.attributeValue("onuse"));
        this.m_maxStackSize = Integer.parseInt(p_element.attributeValue("maxstacksize"));
        this.m_onUseInitialDelay = Integer.parseInt(p_element.attributeValue("onuseinitialdelay"));
        this.m_damageProc = Double.parseDouble(p_element.attributeValue("damageproc"));
        this.m_physicalDamageProc = CUtility.stringToBool(p_element.attributeValue("physicalDamageProc"));

        this.m_whiteAttack = CUtility.stringToBool(p_element.attributeValue("whiteattack"));
        this.m_specialAttack = CUtility.stringToBool(p_element.attributeValue("specialattack"));
        this.m_meleeFinisher = CUtility.stringToBool(p_element.attributeValue("meleefinisher"));
        this.m_spellFinisher = CUtility.stringToBool(p_element.attributeValue("spellfinisher"));
        this.m_deadlyPoison = CUtility.stringToBool(p_element.attributeValue("deadlypoison"));
        this.m_instantPoison = CUtility.stringToBool(p_element.attributeValue("instantpoison"));
        this.m_woundPoison = CUtility.stringToBool(p_element.attributeValue("woundpoison"));
        this.m_anestheticPoison = CUtility.stringToBool(p_element.attributeValue("anestheticpoison"));
        this.m_rupture = CUtility.stringToBool(p_element.attributeValue("rupture"));
        this.m_onHit = CUtility.stringToBool(p_element.attributeValue("onhit"));
        this.m_onCrit = CUtility.stringToBool(p_element.attributeValue("oncrit"));
        this.m_stats.parseXML(p_element);
    }
}
